﻿#region 命名空间

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.IO;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using WinnerCMS.BLL;
using WinnerCMS.Common;
using WinnerCMS.Model;
using WinnerCMS.Model.Attribute;
using Xml = WinnerCMS.Common.Xml;

#endregion

namespace WinnerCMS.WebUI.Admin.Template
{
    [Access("263001")]
    public partial class Up : WebFactory.PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Winrar.Exists() == false)
                {
                    //Uploader1.Visible = false; 
                    this.lblErr.Visible = true;
                    this.lblErr.Text = "对不起服务器没有安装Winrar";
                }
                else
                {
                    this.lblErr.Visible = false;
                }
            }
        }

        private string GetValue(string Key, string FullName)
        {
            FullName = FullName.Replace(Server.MapPath(@"~\"), "");

            return Xml.GetConfig(Key, FullName + @"\info.config");
        }

        protected void AsyncFileUpload1_UploadedComplete(object sender, AjaxControlToolkit.AsyncFileUploadEventArgs args)
        {
            var path = Server.MapPath("~/App_Data/Temp/");
            if (Directory.Exists(path) == false)
            {
                Directory.CreateDirectory(path);
            }

            var fileGuid = Guid.NewGuid().ToString("N");
            this.AsyncFileUpload1.SaveAs(path + fileGuid + args.FileName);

            var rar = new Winrar();
            rar.UnCompress(Server.MapPath("~/Templates/"), Server.MapPath("~/Temp/"), fileGuid + args.FileName);


            var di = new DirectoryInfo(Server.MapPath("~/Templates/"));

            var dt = DateTime.MinValue;

            DirectoryInfo upDir = null;
            foreach (var d in di.GetDirectories())
            {
                if (d.CreationTime > dt)
                {
                    upDir = d;
                }
            }
            if (upDir == null)
            {
                MsgBox.Message("程序找不到你刚刚上传的模板,请自己录入模板集的信息.", this.UpdatePanel1);
            }
            else
            {
                var model = new WinnerCMS.Model.Template
                {
                    TemplateName = GetValue("TemplateName", upDir.FullName),
                    TemplatePath = GetValue("TemplatePath", upDir.FullName),
                    Style = GetValue("Style", upDir.FullName),
                    Remark = GetValue("Remark", upDir.FullName)
                };


                var res = new TemplatesManage().Add(model);

                if (string.IsNullOrEmpty(res.Errors) == false)
                {
                    MsgBox.Message(res.Errors, this.UpdatePanel1);
                }
                else
                {
                    Response.Redirect("Manage.aspx");
                }
            }
        }
    }
}